package com.platform.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.core.text.Convert;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.module.domain.LinkConcentratorMeter;
import com.platform.module.domain.LinkConcentratorValve;
import com.platform.module.mapper.LinkConcentratorValveMapper;
import com.platform.module.service.ILinkConcentratorValveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 集中器和阀门关联Service业务层处理
 *
 * @author wangqi
 * @date 2020-08-01
 */
@Service
public class LinkConcentratorValveServiceImpl extends ServiceImpl<LinkConcentratorValveMapper,LinkConcentratorValve> implements ILinkConcentratorValveService
{
    @Autowired
    private LinkConcentratorValveMapper linkConcentratorValveMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 将阀门和采集器关联关系存放到redis
     */
    @PostConstruct
    public void init() {
        List<LinkConcentratorValve> list = linkConcentratorValveMapper.selectList(new QueryWrapper<>());
        for(LinkConcentratorValve concentratorValve : list){
            redisUtil.hset("concentratorValve", concentratorValve.getValveId(), concentratorValve.getConcentratorId());
        }
    }

    /**
     * 新增集中器和阀门关联
     *
     * @param linkConcentratorValve 集中器和阀门关联
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLinkConcentratorValve(LinkConcentratorValve linkConcentratorValve)
    {
        redisUtil.hset("concentratorValve", linkConcentratorValve.getValveId(), linkConcentratorValve.getConcentratorId());
        return linkConcentratorValveMapper.insert(linkConcentratorValve);
    }

    /**
     * 修改集中器和阀门关联
     *
     * @param linkConcentratorValve 集中器和阀门关联
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLinkConcentratorValve(LinkConcentratorValve linkConcentratorValve)
    {
        redisUtil.hset("concentratorValve", linkConcentratorValve.getValveId(), linkConcentratorValve.getConcentratorId());
        return linkConcentratorValveMapper.updateConcentratorValve(linkConcentratorValve);
    }
}
